<template>
	<view style="height: 100%;background-color: white;">
		<!-- 选择城市 -->
		<view :class="'search-box ' + grayscale">
			<form @submit.stop.prevent="searchSubmit" form-type="search">
				<view class="form">
					<image class="icon" :src="masterDomain + '/static/images/search_icon_city.png?v=' + imgVersion"
						mode="widthFix" />
					<input class="keywords" @input="input" maxlength="20" name="keywords" :value="keywords"
						placeholder="输入城市名" confirm-type="search" @confirm="searchSubmit" />
					<image v-if="keywords" @tap="clean" class="clean"
						:src="masterDomain + '/static/images/icon-close.png?v=' + imgVersion" mode="widthFix" />
				</view>
			</form>
		</view>
		

		<view :class="'main ' + grayscale" v-if="!keywords">
			<scroll-view class="listBox" :scroll-y="true" :scroll-into-view="toView" :enable-back-to-top="true"
				:enhanced="true" :style="'height:calc(100vh - 130rpx)'">
				<block v-if="globalData.appConfig && globalData.appConfig.site_city_auto_location  ">
					
					<view class="table-tit no-select">当前定位</view>
					<view class="local">
						<block v-if="localData.name">
							<view class="text" :data-domain="localData.data" :data-cityid="localData.cityid"
								:data-name="localData.name" @tap="changeCity">
								<image :src="masterDomain + '/static/images/local_icon.png?v=' + imgVersion"
									mode="widthFix" />
								{{ localData.name }}
							</view>
						</block>
						<block v-else>
							<view class="text">
								<image :src="masterDomain + '/static/images/local_icon.png?v=' + imgVersion"
									mode="widthFix" />
								定位中..
							</view>
						</block>
						<text class="localBtn" data-type="click" @tap="getLnglat" hidden>重新定位</text>
					</view>
				</block>
				<view class="table-city" v-if="cityList">
					<!-- 热门城市 -->
					<view class="hot-city" v-if="hotList && hotList.length && site_city_grouptype != undefined"
						id="hot">
						<view class="table-tit table-tit-hot" v-if="site_city_grouptype != undefined">
							{{ site_city_grouptype == 1 ? '我的周边' : '热门城市' }}
						</view>
						<view class="table-list table-list-hot fn-clear">
							<block v-for="(item, index) in hotList" :key="index">
								<text :data-domain="item.data" :data-cityid="item.cityid" :data-name="item.name"
									@tap="changeCity">{{ item.name }}</text>
							</block>
						</view>
					</view>

					<block v-for="(item, index) in cityList" :key="index">
						<view :id="item.szm">
							<view class="table-tit">{{ item.szm }}</view>
							<view class="table-list">
								<block v-for="(item, index1) in item.data" :key="index1">
									<text :data-domain="item.data" :data-cityid="item.cityid" :data-name="item.name"
										@tap="changeCity">{{ item.name }}</text>
								</block>
							</view>
						</view>
					</block>
				</view>
				<view class="placeHolder" hover-class="none" :hover-stop-propagation="false"></view>
			</scroll-view>

			<view class="szm navbar jump-ul" v-if="szmList">
				<text data-id="hot" v-if="hotList" @touchstart.stop.prevent="touchstart"
					@touchmove.stop.prevent="touchmove" @touchend.stop.prevent="touchend">#</text>
				<block v-for="(item, index) in szmList" :key="index">
					<text :data-id="item" @touchstart.stop.prevent="touchstart" @touchmove.stop.prevent="touchmove"
						@touchend.stop.prevent="touchend">{{ item }}</text>
				</block>
			</view>
		</view>

		<view :class="'search-list ' + grayscale" v-if="keywords">
			<block v-for="(item, index) in searchList" :key="index">
				<text :data-domain="item.data" :data-cityid="item.cityid" :data-name="item.name"
					@tap="changeCity">{{ item.name }}</text>
			</block>
		</view>

		<view class="letter" v-if="indexShow">{{ indexSzm }}</view>

		<view :class="'cityMask ' + grayscale + ' ' + (showPop ? 'show' : '')"></view>
		<view :class="'cityScrollBox ' + grayscale + ' ' + (showPop ? 'show' : '')">
			<view class="h2">
				请选择
				<view class="close_pop a" @tap="hidePop">
					<image :src="masterDomain + '/static/images/close_private_phone.png?v=' + imgVersion"
						mode="aspectFit"></image>
				</view>
			</view>
			<view class="cityScroll">
				<view class="tabBox">
					<view v-if="ind > 0 && ind < 4"
						:class="(ind == 1 ? 'city' : ind == 2 ? 'district' : 'town') + ' ' + (currshow == ind - 1 ? 'on_chose' : '') + ' span'"
						:data-name="item" :data-index="ind - 1" @tap="changeShow" v-for="(item, ind) in areaArr"
						:key="ind">
						<text v-if="ind == 1">{{ city.name ? city.name : item }}</text>

						<text v-else-if="ind == 2 && city.name != ''">{{ district.name ? district.name : item }}</text>

						<text v-else-if="district.name != ''">{{ town.name ? town.name : item }}</text>
					</view>
					<view class="s" :style="'transform:translateX(' + offsetLeft + 'px)'"></view>
				</view>
				<view class="cityListBox">
					<view
						:class="'ul cityList ' + (currshow == 1 ? 'parentUl' : '') + ' ' + (currshow == 2 ? 'fn-hide' : '')">
						<view :class="'li ' + (item.cityid == currChoseCity.cityid ? 'active' : '')"
							v-for="(item, index) in lowerCityList" :key="index">
							<view class="prev"
								:style="'background-image:url(' + masterDomain + '/static/images/radius1.png?v=' + imgVersion + ')'">
							</view>

							<text :data-domain="JSON.stringify(item)" :data-cityid="item.cityid" :data-name="item.name"
								class="a" data-pid="0" @tap="changeCityList">
								{{ item.name }}
							</text>

							<view class="next"
								:style="'background-image:url(' + masterDomain + '/static/images/radius2.png?v=' + imgVersion + ')'">
							</view>
						</view>
					</view>
					<view
						:class="'ul cityList ' + (currshow == 0 ? 'fn-hide' : '') + ' ' + (currshow == 1 ? 'currOnUl' : '') + ' ' + (currshow == 2 ? 'parentUl' : '')">
						<view :class="'li ' + (item.cityid == currChoseCity.districtid ? 'active' : '')"
							v-for="(item, index) in lowerdistrictList" :key="index">
							<view class="prev"
								:style="'background-image:url(' + masterDomain + '/static/images/radius1.png?v=' + imgVersion + ')'">
							</view>

							<text :data-domain="JSON.stringify(item)" :data-cityid="item.cityid" :data-name="item.name"
								class="a" data-pid="1" @tap="changeCityList">
								{{ item.name }}
							</text>

							<view class="next"
								:style="'background-image:url(' + masterDomain + '/static/images/radius2.png?v=' + imgVersion + ')'">
							</view>
						</view>
					</view>
					<view
						:class="'ul cityList ' + (currshow != 2 ? 'fn-hide' : '') + '  ' + (currshow == 2 ? 'currOnUl' : '')">
						<view :class="'li ' + (item.cityid == currChoseCity.townid ? 'active' : '')"
							v-for="(item, index) in lowertownList" :key="index">
							<text :data-domain="JSON.stringify(item)" :data-cityid="item.cityid" :data-name="item.name"
								class="a" data-pid="2" @tap="changeCityList">
								{{ item.name }}
							</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<private_tip :privateShow="privateb"></private_tip>
	</view>
</template>
<script>
	var _this;
	//2023.6.6：该页面使用说明（仅对于原生页面而言）：1.上级页面必传origin=1;2.因为选择完之后页面需要刷新，而这个操作要在上级页面完成（用onShow）。
	//切换城市的函数请搜索：changeCity
	//获取应用实例 
	const app = getApp();
	import myPosition from '../../utils/publicLocation.js'
	export default {
		data() {
			return {
				privateb: false,
				titleText: '选择城市',
				masterDomain: app.globalData.masterDomain,

				//系统主域名
				imgVersion: app.globalData.imgVersion,

				//图片版本号
				imgPath: app.globalData.imgPath,

				globalData:app.globalData,
				localData: [],

				//定位数据
				keywords: '',

				//搜索关键字
				searchList: [],

				//搜索列表
				module: '',

				//模块
				cityData: [],

				//城市数据
				cityList: [],

				//城市列表
				szmList: [],

				//首字母列表
				hotList: [],

				//热门城市
				toView: 'hot',

				indexShow: false,
				indexSzm: '热门',
				positionEnd: true,

				//是否正在定位中
				site_city_grouptype: app.globalData.appConfig.site_city_grouptype,

				// 城市是否分组
				areaArr: '',

				showPop: false,

				//是否弹窗
				currChoseCity: {
					provid: '',
					cityid: '',
					districtid: '',
					townid: ''
				},

				//当前选择的城市
				lowerCityList: [],

				lowerdistrictList: [],
				lowertownList: [],

				city: {
					name: '',
					id: ''
				},

				district: {
					name: '',
					id: ''
				},

				town: {
					name: '',
					id: ''
				},

				offsetLeft: 0,
				currshow: 0,
				parentLen: 0,
				grayscale: app.globalData.appConfig.sitePageGray == '1' || app.globalData.appConfig.sitePageGray == '2' ?
					'grayscale' : '',
				searchListData: [],

				//搜索内容
				//是否是原生页面
				original: '',

				propName: '',
				undefined: '',
				ind: 0
			};
		}
		/**
		 * 生命周期函数--监听页面加载
		 */
		,
		onLoad: function(options) {
			_this = this;
			if (app.globalData.wxprivate == -1) {
				app.watchG('wxprivate', function(value) {
					_this.wxprivate = value;
					if (value) { //弹出弹窗
						_this.privateb = true;
					} else {
						_this.onload(options);
					}
				})
			} else {
				if (app.globalData.wxprivate) { //弹出弹窗
					_this.privateb = true;
				} else {
					_this.onload(options);
				}
			}
		},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {
			return {
				title: _this.titleText
			};
		},
		onShareTimeline: function() {
			return {
				title: _this.titleText
			};
		},
		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {
			if (!_this.showPop) {
				_this.loadData();
				_this.getLnglat('first');
			}
			uni.stopPullDownRefresh();
		},
		methods: {
			onload(options) {
				if (options.module) {
					//模块
					_this.module = options.module
				}
				if (options.original) {
					//是否是原生
					_this.original = options.original;
				}
				var interval = setInterval(function() {
					if (app.globalData.appConfig && app.globalData.appConfig.cfg_basehost) {
						_this.site_city_grouptype = app.globalData.appConfig.site_city_grouptype;
						_this.areaArr = app.globalData.appConfig.site_areaname;
						_this.indexSzm = '周边'
						clearInterval(interval);
					}
				}, 1000);
				_this.getLnglat('first');
				_this.loadData();
				_this.getSearchCon();
			},
			//加载数据
			loadData() {
				var site_city_grouptype = app.globalData.appConfig.site_city_grouptype;
				var sortBy = function(prop) {
					return function(obj1, obj2) {
						var val1 = obj1[prop];
						var val2 = obj2[prop];
						if (!isNaN(Number(val1)) && !isNaN(Number(val2))) {
							val1 = Number(val1);
							val2 = Number(val2);
						}
						if (val1 < val2) {
							return -1;
						} else if (val1 > val2) {
							return 1;
						} else {
							return 0;
						}
					};
				};
				var dataPrarm = 'service=siteConfig&action=siteCity&module=' + _this.module;
				if (site_city_grouptype) {
					dataPrarm = 'service=siteConfig&action=siteCityFirst&module=' + _this.module;
				}
				var urlData = app.encodeUrl(dataPrarm);
				_this.$ajax.post({
					url: 'include/ajax.php?' + urlData,
				}).then(res => {
					var data = res.data;
					if (data.state == 100) {
						var list = data.info;
						var cityArr = new Array();
						var cityList = [];
						var szmList = [];
						var hotList = [];
						for (var i = 0; i < list.length; i++) {
							var pinyin = list[i].pinyin.substr(0, 1);
							if (cityArr[pinyin] == undefined) {
								cityArr[pinyin] = [];
							}
							cityArr[pinyin].push(list[i]);
						}

						//首字母
						var szmArr = [];
						for (var key in cityArr) {
							var szm = key;
							// 右侧字母数组
							szmArr.push(key);
						}

						// 右侧字母填充
						szmArr.sort();
						szmList = szmArr;
						for (var i = 0; i < szmArr.length; i++) {
							cityArr[szmArr[i]].sort(sortBy('cityid'));
							var cityListData = [];
							for (var j = 0; j < cityArr[szmArr[i]].length; j++) {
								cityListData.push({
									name: cityArr[szmArr[i]][j].name,
									cityid: cityArr[szmArr[i]][j].cityid,
									data: JSON.stringify(cityArr[szmArr[i]][j])
								});
								if (cityArr[szmArr[i]][j].hot == 1 && site_city_grouptype != 1) {
									hotList.push({
										name: cityArr[szmArr[i]][j].name,
										cityid: cityArr[szmArr[i]][j].cityid,
										data: JSON.stringify(cityArr[szmArr[i]][j])
									});
								}
							}
							cityList[szmArr[i]] = cityListData;
						}
						var cityListData = [];
						for (var n in cityList) {
							cityListData.push({
								szm: n,
								data: cityList[n]
							});
						}
						_this.cityData = list;
						_this.cityList = cityListData;
						_this.szmList = szmList;
						_this.hotList = hotList
					}

				})
			},

			// 获取我的周边
			getZB_list(posiInfo) {
				var dataPrarm = 'service=siteConfig&action=nearbyCity&lat=' + posiInfo.lat + '&lng=' + posiInfo.lng +
					'&cityid=' + posiInfo.cityid;
				var urlData = app.encodeUrl(dataPrarm);
				_this.$ajax.post({
					url: 'include/ajax.php?' + urlData,
				}).then(res => {
					var data = res.data;
					if (data.state == 100) {
						var hotList = [];
						for (var i = 0; i < data.info.length; i++) {
							var list = data.info[i];
							hotList.push({
								name: list.name,
								cityid: list.cityid,
								data: JSON.stringify(list)
							});
						}
						_this.hotList = hotList
					}
				})
			},

			changeShow(e) {
				var ind = e.currentTarget.dataset.index;
				if (ind == 0) {
					var clsname = '.city';
				} else {
					if (ind == 1) {
						var clsname = '.district';
					} else {
						var clsname = '.town';
					}
				}
				var clsBefore = ind == 0 ? 'provid' : ind == 1 ? 'cityid' : 'districtid';
				if (_this.currChoseCity[clsBefore] != '') {
					var query = uni.createSelectorQuery();
					query
						.select(clsname)
						.boundingClientRect(function(rect) {
							var left = rect.left + rect.width / 2;
							_this.offsetLeft = left;
							_this.currshow = ind;
						})
						.exec();
				}
			},

			changeShowAuto(ind) {
				var clsname = ind == 0 ? '.city' : ind == 1 ? '.district' : '.town';
				var clsBefore = ind == 0 ? 'provid' : ind == 1 ? 'cityid' : 'districtid';
				if (_this.currChoseCity[clsBefore] != '') {
					var query = uni.createSelectorQuery();
					query
						.select(clsname)
						.boundingClientRect(function(rect) {
							var left = rect.left + rect.width / 2;
							_this.offsetLeft = left;
							_this.currshow = ind
						})
						.exec();
				}
			},

			//提交搜索
			searchSubmit(e) {
				var keywords = e.detail.value;
				_this.keywords = keywords
				_this.search();
			},

			//输出内容有变
			input(e) {
				var keywords = e.detail.value;
				_this.keywords = keywords
				_this.search();
			},

			// 获取要搜索的城市
			getSearchCon() {
				var dataPrarm = 'service=siteConfig&action=siteCity&module=' + _this.module;
				var urlData = app.encodeUrl(dataPrarm);
				_this.$ajax.post({
					url: 'include/ajax.php?' + urlData,
				}).then(res => {
					var data = res.data;
					if (data.state == 100) {
						var list = data.info;
						_this.searchListData = list
					}
				})
			},

			//搜索
			search() {
				var keywords = _this.keywords.replace(/\s+/g, '');
				var cityData = _this.searchListData;
				if (keywords) {
					var searchList = [];
					for (var m = 0; m < cityData.length; m++) {
						var name = cityData[m].name;
						var pinyin = cityData[m].pinyin;
						if (name.indexOf(keywords) >= 0 || pinyin.indexOf(keywords) >= 0) {
							searchList.push({
								name: name,
								cityid: cityData[m].cityid,
								data: JSON.stringify(cityData[m])
							});
						}
					}
					_this.searchList = searchList
				}
			},

			//清空搜索关键字
			clean(e) {
				_this.keywords = ''
			},

			//首字母快速定位
			touchstart(e) {
				uni.vibrateShort();
				var site_city_grouptype = app.globalData.appConfig.site_city_grouptype;
				_this.toView = e.target.dataset.id.toLowerCase();
				_this.indexy = e.touches[0].pageY;
				_this.indexShow = true;
				_this.indexSzm = e.target.dataset.id == 'hot' ? (site_city_grouptype == 1 ? '周边' : '热门') : e.target
					.dataset.id;
			},

			//首字母快速定位
			touchmove(e) {
				var y = this.getArrIndex(e.target.dataset.id);
				var indexY = e.touches[0].pageY;
				if (this.getArrEnglish(Math.round((indexY - this.indexy) / 15), y)) {
					var szm = this.getArrEnglish(Math.round((indexY - this.indexy) / 15), y);
					_this.toView = szm == '热门' || szm == '周边' ? 'hot' : szm.toLowerCase();
					_this.indexSzm = szm;
				}
			},
			//首字线快速定位
			touchend(e) {
				_this.indexShow = false
			},

			//获取touchstart字母数组角标
			getArrIndex(english) {
				var szmList = this.szmList;
				for (var x = 0; x < szmList.length; x++) {
					if (english == szmList[x]) {
						return x;
					}
				}
			},

			//num 移动了多少位 index 当前字母,返回当前触摸位置节点的字母
			getArrEnglish(num, index) {
				var szmList = this.szmList;
				var english = szmList[index + num];
				var site_city_grouptype = app.globalData.appConfig.site_city_grouptype;
				if (!(1 > num + index > 26)) {
					return english == undefined ? (site_city_grouptype == 1 ? '周边' : '热门') : english;
				} else {
					return AAA;
				}
			},

			// 获取当前城市的下一级
			getLowerList(id, type) {
				var dataPrarm = 'service=siteConfig&action=siteCityById&id=' + id;
				var urlData = app.encodeUrl(dataPrarm);
				_this.$ajax.post({
					url: 'include/ajax.php?' + urlData,
				}).then(res => {
					var data = res.data;
					if (data.state == 100) {
						if (type != undefined) {
							var propName = 'lowerCityList';
							if (type == 0) {
								propName = 'lowerCityList';
							} else if (type == 1) {
								propName = 'lowerdistrictList';
							} else {
								propName = 'lowertownList';
							}
							_this[propName] = data.info;
							_this.showPop = true;
							_this.currshow = type;
							_this.changeShowAuto(type + 1);
						} else {
							if (!_this.showPop) {
								_this.lowerCityList = data.info;
								_this.showPop = true;
								_this.lowerdistrictList = [];
								_this.lowertownList = [];
								_this.currshow = 0;
								if (_this.currChoseCity.cityid == '') {
									var query = uni.createSelectorQuery();
									query
										.select('.city')
										.boundingClientRect(function(rect) {
											var left = rect.left + rect.width / 2;
											_this.offsetLeft = left
										})
										.exec();
								}
							} else {
								if (_this.currshow == 1) {
									_this.lowerdistrictList = data.info
								} else {
									_this.lowertownList = data.info
								}
							}
						}
					}
				})
			},

			hidePop() {
				_this.showPop = false
			},

			changeCityList(e) {
				var ind = e.currentTarget.dataset.pid;
				var cityInfo = {
					name: '',
					id: ''
				};
				var city = {
					name: e.currentTarget.dataset.name,
					id: e.currentTarget.dataset.cityid
				};
				var key = '';
				if (ind == 0) {
					cityInfo = {
						provid: _this.currChoseCity.provid,
						cityid: e.currentTarget.dataset.cityid,
						districtid: '',
						townid: ''
					};
					key = 'city';
					_this.district = {
						name: '',
						id: ''
					};
					_this.town = {
						name: '',
						id: ''
					};
				} else if (ind == 1) {
					cityInfo = {
						provid: _this.currChoseCity.provid,
						cityid: _this.currChoseCity.cityid,
						districtid: e.currentTarget.dataset.cityid,
						townid: ''
					};
					key = 'district';
					_this.town = {
						name: '',
						id: ''
					}
				} else {
					cityInfo = {
						provid: _this.currChoseCity.provid,
						cityid: _this.currChoseCity.cityid,
						districtid: _this.currChoseCity.districtid,
						townid: e.currentTarget.dataset.cityid
					};
					key = 'town';
				}
				_this.currChoseCity = cityInfo;
				_this.currshow = ind * 1 + 1;
				_this[key] = city;
				_this.changeShowAuto(ind * 1 + 1);
				_this.changeCity(e);
			},

			//切换城市
			changeCity(e) {
				var dataset = e.target.dataset;
				var name = dataset.name;
				var cityid = dataset.cityid;
				var domain = dataset.domain;
				if (!cityid || !domain) {
					return false;
				}
				var JS_domain = {};
				if (typeof domain == 'string') {
					JS_domain = JSON.parse(domain);
				} else if (typeof domain == 'object') {
					JS_domain = domain;
				}
				if (JS_domain && JS_domain.son >= 1) {
					if (!_this.showPop) {
						var obj_init = {
							name: '',
							id: ''
						};
						_this.city = obj_init;
						_this.district = obj_init;
						_this.town = obj_init;
						for (var item in _this.currChoseCity) {
							_this['currChoseCity.' + item] = ''
						}
					}
					if (_this.currChoseCity.provid == '') {
						_this['currChoseCity.provid'] = JS_domain.cityid ? JS_domain.cityid : JS_domain.id
					}
					if (JS_domain.parent && JS_domain.parent.ids && JS_domain.parent.ids.length > 1) {
						var obj_init = {
							name: '',
							id: ''
						};
						_this.city = obj_init;
						_this.district = obj_init;
						_this.town = obj_init;
						// 有父级
						for (var i = 0; i < JS_domain.parent.ids.length; i++) {
							var id = JS_domain.parent.ids[i];
							var cityInfo = {
								name: JS_domain.parent.names[i + 1] ? JS_domain.parent.names[i + 1] : '',
								id: JS_domain.parent.ids[i + 1] ? JS_domain.parent.ids[i + 1] : ''
							};
							var key = i == 0 ? 'city' : i == 1 ? 'district' : 'town';
							var key2 = i == 0 ? 'provid' : i == 1 ? 'cityid' : i == 2 ? 'district' : 'townid';
							_this[key] = cityInfo;
							_this.currshow = i;
							_this['currChoseCity.' + key2] = JS_domain.parent.ids[i];
							_this.getLowerList(id, i);
						}
					} else {
						_this.getLowerList(JS_domain.cityid ? JS_domain.cityid : JS_domain.id);
					}
					return false;
				}
				uni.setStorage({
					key: 'cityInfo',
					data: JSON.parse(domain)
				});

				console.log(domain)

				app.globalData.cityInfo = JSON.parse(domain);
				var module = _this.module;
				let pages = getCurrentPages();
				// 声明一个pages使用getCurrentPages方法
				let referpage = pages[pages.length - 2] //上个页面
				if (module == undefined || module == '' || module == 'siteConfig') {
					uni.setStorageSync('manualChange', 1);
					if (referpage) {
						//关闭当前页面，跳转到上一页面
						uni.navigateBack()
					} else {
						uni.redirectTo({
							url: '/pages/index/index'
						});
					}
				} else if (module) {
					uni.setStorageSync('manualChange', 1);
					uni.setStorage({
						key: 'cityname',
						data: name,
					});
					uni.setStorage({
						key: 'cityid',
						data: cityid,
					})
					if (referpage) {
						uni.navigateBack();
					} else {
						uni.redirectTo({
							url: `/pages/packages/${module}/index/index`
						});
					}
				} else if (Boolean(_this.original)) {
					uni.setStorageSync('manualChange', 1);
					//有原生页面
					uni.navigateBack({
						delta: 1,
					});
				} else {
					domain = JSON.parse(domain);
					uni.redirectTo({
						url: '/pages/redirect/index?url=' + encodeURIComponent(domain.url)
					});
				}
			},

			// 重新定位
			getLnglat(type) {
				var positionEnd = _this.positionEnd;

				//如果正在定位中，则不再重新定位
				if (positionEnd && type != 'first') {
					return;
				}
				if (type.currentTarget) {
					type = type.currentTarget.dataset.type;
				}
				_this.positionEnd = true;
				// 平台切换

				if (app.globalData.platForm != 'web') {
					uni.getSetting({
						success(res) {
							if (app.globalData.siteDebug) {
								uni.showModal({
									title: '授权成功',
									content: JSON.stringify(res.authSetting),
									success(res) {
										if (res.confirm) {
											console.log('用户点击确定');
										} else if (res.cancel) {
											console.log('用户点击取消');
										}
									}
								});
							}
							if (res.authSetting.hasOwnProperty('scope.userLocation') && !res.authSetting[
									'scope.userLocation']) {
								//获取过授权定位 并且拒绝了
								uni.openSetting({
									success(obj) {
										console.log(obj);
									}
								});
							} else {
								myPosition.getPosition(function(data) {
									if (app.globalData.siteDebug) {
										uni.showModal({
											title: '定位结果',
											content: JSON.stringify(data),
											success(res) {
												if (res.confirm) {
													console.log('用户点击确定');
												} else if (res.cancel) {
													console.log('用户点击取消');
												}
											}
										});
									}
									if (data.errMsg == 'getLocation:ok' || data.latitude || data
										.longitude) {
										//定位成功
										if (type == 'click') {
											//点击重新定位了
											uni.showLoading({
												title: '加载中'
											});
											app.chooseLoaction(
												function(result) {
													// 以上均为更新本地分站经纬度信息，以下为外卖地址更新
													app.getCityInfo(result.longitude, result.latitude,
														'gcj02ll',
														function(results) {
															_this.positionEnd = false
															app.getCurrCityInfo(results,
																function(data) {
																	if (data.state == 100) {
																		var localData = {
																			name: data.info
																				.name,
																			cityid: data
																				.info
																				.cityid,
																			data: JSON
																				.stringify(
																					data
																					.info)
																		};
																		_this.localData =
																			localData
																		// 分组需要查看周边
																		if (app.globalData
																			.appConfig
																			.site_city_grouptype
																		) {
																			_this.getZB_list(
																				data
																				.info);
																		}
																		uni.setStorageSync(
																			'manualChange',
																			1);
																		uni.navigateBack();
																	} else {
																		if(_this.globalData.appConfig && _this.globalData.appConfig.site_city_auto_location == 1){

																			uni.showToast({
																				icon: 'none',
																				title: '此位置暂未开通服务，敬请期待！',
																				duration: 2000
																			});
																		}
																	}
																});
														});
												},
												function() {
													_this.positionEnd = false
												}
											);
										} else {
											var latlng = data.latitude + ',' + data.longitude;
											// 判断当前定位城市是否开通分站
											var dataPrarm =
												'service=siteConfig&action=getLocationByGeocoding&location=' +
												latlng;
											var urlData = app.encodeUrl(dataPrarm);
											_this.$ajax.post({
												url: 'include/ajax.php?' + urlData,
											}).then(res => {
												_this.positionEnd = false
												var data = res.data;
												if (data.state == 100) {
													var cityInfoArr = data.info;
													var dataPrarm =
														'service=siteConfig&action=verifyCity&region=' +
														cityInfoArr.province +
														'&city=' +
														cityInfoArr.city +
														'&town=' +
														cityInfoArr.town +
														'&district=' +
														cityInfoArr.district;
													var urlData = app.encodeUrl(
														dataPrarm);
													_this.$ajax.post({
														url: 'include/ajax.php?' +
															urlData,
													}).then(res => {
														var data = res
															.data;
														if (data.state ==
															100) {
															var localData = {
																name: data
																	.info
																	.name,
																cityid: data
																	.info
																	.cityid,
																data: JSON
																	.stringify(
																		data
																		.info
																	)
															};
															_this
																.localData =
																localData

															// 分组需要查看周边
															if (app
																.globalData
																.appConfig
																.site_city_grouptype
															) {
																_this
																	.getZB_list(
																		data
																		.info
																	);
															}
														} else {
															if(_this.globalData.appConfig && _this.globalData.appConfig.site_city_auto_location == 1){
																uni.showToast({
																	icon: 'none',
																	title: '此位置暂未开通服务，敬请期待！'
																});
															}
															var localData = {
																name: data
																	.info
															};
															_this
																.localData =
																localData
														}
													})
												} else {
													uni.showToast({
														icon: 'none',
														title: data.info
													});
												}
											})
										}
									} else {
										_this.positionEnd = false
										var localData = {
											name: '定位失败'
										};
										_this.localData = localData
									}
								}, 1);
							}
						}
					});
				} else {
					myPosition.getPosition(function(data) {
						if (app.globalData.siteDebug) {
							uni.showModal({
								title: '定位结果',
								content: JSON.stringify(data),
								success(res) {
									if (res.confirm) {
										console.log('用户点击确定');
									} else if (res.cancel) {
										console.log('用户点击取消');
									}
								}
							});
						}
						if (data.errMsg == 'getLocation:ok' || data.latitude || data
							.longitude) {
							//定位成功
							if (type == 'click') {
								//点击重新定位了
								uni.showLoading({
									title: '加载中'
								});
								app.chooseLoaction(
									function(result) {
										// 以上均为更新本地分站经纬度信息，以下为外卖地址更新
										app.getCityInfo(result.longitude, result.latitude,
											'gcj02ll',
											function(results) {
												_this.positionEnd = false
												app.getCurrCityInfo(results,
													function(data) {
														if (data.state == 100) {
															var localData = {
																name: data.info
																	.name,
																cityid: data
																	.info
																	.cityid,
																data: JSON
																	.stringify(
																		data
																		.info)
															};
															_this.localData =
																localData
															// 分组需要查看周边
															if (app.globalData
																.appConfig
																.site_city_grouptype
															) {
																_this.getZB_list(
																	data
																	.info);
															}
															uni.setStorageSync(
																'manualChange',
																1);
															uni.navigateBack();
														} else {
															if(_this.globalData.appConfig && _this.globalData.appConfig.site_city_auto_location == 1){
																uni.showToast({
																	icon: 'none',
																	title: '此位置暂未开通服务，敬请期待！',
																	duration: 2000
																});
															}
														}
													});
											});
									},
									function() {
										_this.positionEnd = false
									}
								);
							} else {
								var latlng = data.latitude + ',' + data.longitude;
								// 判断当前定位城市是否开通分站
								var dataPrarm =
									'service=siteConfig&action=getLocationByGeocoding&location=' +
									latlng;
								var urlData = app.encodeUrl(dataPrarm);
								_this.$ajax.post({
									url: 'include/ajax.php?' + urlData,
								}).then(res => {
									_this.positionEnd = false
									var data = res.data;
									if (data.state == 100) {
										var cityInfoArr = data.info;
										var dataPrarm =
											'service=siteConfig&action=verifyCity&region=' +
											cityInfoArr.province +
											'&city=' +
											cityInfoArr.city +
											'&town=' +
											cityInfoArr.town +
											'&district=' +
											cityInfoArr.district;
										var urlData = app.encodeUrl(
											dataPrarm);
										_this.$ajax.post({
											url: 'include/ajax.php?' +
												urlData,
										}).then(res => {
											var data = res
												.data;
											if (data.state ==
												100) {
												var localData = {
													name: data
														.info
														.name,
													cityid: data
														.info
														.cityid,
													data: JSON
														.stringify(
															data
															.info
														)
												};
												_this
													.localData =
													localData

												// 分组需要查看周边
												if (app
													.globalData
													.appConfig
													.site_city_grouptype
												) {
													_this
														.getZB_list(
															data
															.info
														);
												}
											} else {
												if(_this.globalData.appConfig && _this.globalData.appConfig.site_city_auto_location == 1){

													uni.showToast({
														icon: 'none',
														title: '此位置暂未开通服务，敬请期待！'
													});
												}
												var localData = {
													name: data
														.info
												};
												_this
													.localData =
													localData
											}
										})
									} else {
										uni.showToast({
											icon: 'none',
											title: data.info
										});
									}
								})
							}
						} else {
							_this.positionEnd = false
							var localData = {
								name: '定位失败'
							};
							_this.localData = localData
						}
					}, 1);
				}
			}
		}
	};
</script>
<style>
	@import './index.css';
</style>